package com.xhwl.centiotdata.suppliersystem.dnake;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.emq.dnake.DnakeMqttClient;
import com.xhwl.centiotdata.pojo.dto.talkback.TalkBackIntegrationConfigDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.xhwl.centiotdata.pojo.constant.DnakeConstant.FROM;
import static com.xhwl.centiotdata.pojo.constant.DnakeConstant.VOIP_WRITE;

/**
 * @description:
 * @author: wuyuhao
 * @createDate: 2023/5/15 15:55
 */
@Service
@Slf4j
public class TalkDnakeHandler {

    @Resource
    private DnakeMqttClient dnakeMqttClient;

    public void setSip(TalkBackIntegrationConfigDTO configDTO, String uid, String sip) {
        JSONObject talk = configDTO.getTalk();
        String ip = talk.getString("ip");
        String port = talk.getString("port");
        String password = talk.getString("password");
        String remoteOpenTopic = String.format(VOIP_WRITE, uid);
        JSONObject request = new JSONObject();
        request.put("msgId", "");
        request.put("from", FROM);
        request.put("to", uid);
        request.put("action", "voipWrite");
        JSONObject params = new JSONObject();
        /**
         * {
         * 	"msgId": "",
         * 	"action": "voipWrite",
         * 	"from": "xhwl",
         * 	"to": "AB900DX904cab2837a70"
         * 	"params": {
         * 		"proxy": "sip:218.245.102.125",
         * 		"passwd": "12345678",
         * 		"realm": "218.245.102.125:5060",
         * 		"ex_user": "1019",
         * 		"ex_enable": 1
         *        }
         * }
         */
        // Sip扩展服务器的启用 0:不启动 1:启动
        params.put("ex_enable", 1);
        // 设备域
        params.put("realm", ip + ":" + port);
        // 用户名
        params.put("ex_user", sip);
        // 用户密码
        params.put("passwd", password);
        // Sip服务器
        params.put("proxy", "sip:" + ip);
        request.put("params", params);
        dnakeMqttClient.publish(remoteOpenTopic, request.toJSONString());
    }

}
